﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System.Text.RegularExpressions;
using System.Collections;
using System.Text;
using System.Web.SessionState;


namespace CNKI.TPI.Web.UI.Controllers.Submit
{
    public class AddNaviController : BaseController
    {
        int dbID = 0;
        string dbCode = "";
        string sysid = "";
        string dbName = "";
        string fieldname = "";
        string fieldvalue = "";
        int categoryType = 0;

        INaviService naviService = null;
        ISearchService searchService = null;
        public AddNaviController(INaviService naviService, IUserDBDAO userDBDAO, ISearchService searchService)
        {
            this.naviService = generator.CreateInterfaceProxyWithTarget<INaviService>(naviService, new IInterceptor[] { new LogInterceptor() });
            this.searchService = generator.CreateInterfaceProxyWithTarget<ISearchService>(searchService, new IInterceptor[] { new LogInterceptor() });
        }


        public ActionResult Index()
        {
            //接收参数
            LoadParameter();

            //绑定数据库分类导航
            IList<CLS> categoryTypeList = naviService.GetCategoryType(dbCode, dbID);
            ViewBag.CategoryType = categoryTypeList;

            //保存导航ID
            int ClsID = naviService.GetClsID(dbCode, fieldname);
            if (ClsID != 0)
            {
                ViewBag.ClsID = ClsID;
            }

            return View();
        }


        private void LoadParameter()
        {
            if (!String.IsNullOrEmpty(Request["dbID"]))
            {
                dbID = int.Parse(Request["dbID"]);
                ViewBag.dbID = dbID;
            }
            if (!String.IsNullOrEmpty(Request["dbCode"]))
            {
                dbCode = Request["dbCode"];
                ViewBag.dbCode = dbCode;
            }
            if (!String.IsNullOrEmpty(Request["dbName"]))
            {
                dbName = Server.UrlDecode(Request["dbName"].ToString());
                ViewBag.dbName = dbName;
            }
            if (!String.IsNullOrEmpty(Request["type"]))
            {
                categoryType = int.Parse(Request["type"]);
            }
            if (!String.IsNullOrEmpty(Request["sysid"]))
            {
                sysid = Request["sysid"];
                ViewBag.sysid = sysid;
            }
            if (!String.IsNullOrEmpty(Request["fieldname"]))
            {
                fieldname = Server.UrlDecode(Request["fieldname"].ToString());
                ViewBag.fieldname = fieldname;
            }
            if (!String.IsNullOrEmpty(Request["fieldvalue"]))
            {
                fieldvalue = Server.UrlDecode(Request["fieldvalue"].ToString());
                ViewBag.fieldvalue = fieldvalue;
            }
 

            //保存需要显示导航文本信息的控件名称---web.config中配置
            if (ConfigurationManager.AppSettings["Navi"] != null)
            {
                string strNaviName = "";
                string strNavi = ConfigurationManager.AppSettings["Navi"].ToString();
                string[] strArrNavi = strNavi.Split(new string[] { "###" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < strArrNavi.Length; i++)
                {
                    string[] strArrNaviNew = strArrNavi[i].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    if (dbCode.ToString().ToLower() == strArrNaviNew[0].ToString().ToLower() && fieldname.ToString().ToLower() == strArrNaviNew[2].ToString().ToLower())
                    {
                        strNaviName = strArrNaviNew[1].ToString();
                        break;
                    }
                }
                ViewBag.NaviName = strNaviName;
            }
        }

        public ActionResult GetCategroyList()
        {
            LoadParameter();
            IList<CLS_NODE> categoryList = naviService.GetCategoryList(dbCode, categoryType, dbID);
            ViewBag.CategoryList = categoryList;
            return PartialView("CategoryList");
        }

        public JsonResult GetCategroyChilds()
        {
            LoadParameter();
            string strCode = "";
            if (!String.IsNullOrEmpty(Request.QueryString["Code"]))
            {
                strCode = Request.QueryString["Code"].ToString();
            }
            IList<CLS_NODE> categoryList = naviService.GetChildrenCategoryList(dbCode, categoryType, strCode, dbID);
            var res = new JsonResult();
            res.Data = categoryList;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;
        }


        public JsonResult GetNaviInfo()
        {
            LoadParameter();
            string strcheck = "";
            if (!String.IsNullOrEmpty(Request["checkstr"]))
            {
                strcheck = Request["checkstr"].ToString();
            }
            string CheckedList =searchService.processCategoryID(strcheck);
            CheckedList=CheckedList.Replace("?", "");
            return Json(new { item = CheckedList}, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetCategoryInfo(string DbCode, int catid, int dbID)
        {
            IList<CLS_NODE> categoryList = naviService.GetCategoryAllList(DbCode, catid, dbID);
            var res = new JsonResult();
            res.Data = categoryList;
            res.MaxJsonLength = 102400000;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。 
            return res;

        }
    }
}
